from ctaf_core.ctaf_objects.base_event import BaseEvent
from ctaf_core.ctaf_objects.ctaf_general_types import EventHandlerType
from ctaf_core.event_driven_bundle.event_engine import EventEngine
from ctaf_core.ctaf_objects.base_bundle import BaseBundle
from ctaf_facade.enumerations.event_handler_exe_type_num import EventHandlerExeTypeEnum
from ctaf_facade.enumerations.event_type_enum import EventTypeEnum


class EventDrivenBundle(BaseBundle):

    def __init__(self):
        super().__init__("EventDrivenBundle")
        self.event_engine: EventEngine = EventEngine()

    def load_bundle(self):
        self.event_engine.start()


    def put_event(self, event: BaseEvent):
        self.event_engine.put(event)

    def event_handler_register (self, handler:EventHandlerType, event_type:EventTypeEnum = None, execution_type: EventHandlerExeTypeEnum = EventHandlerExeTypeEnum.SYNC_HANDLER):
        if not event_type:
            self.event_engine.register_general(handler, execution_type)
        else:
            self.event_engine.register(event_type, handler, execution_type)

    def event_handler_unregister (self, handler:EventHandlerType, event_type:EventTypeEnum = None, execution_type: EventHandlerExeTypeEnum = EventHandlerExeTypeEnum.SYNC_HANDLER):
        if not event_type:
            self.event_engine.unregister_general(handler, execution_type)
        else:
            self.event_engine.unregister(event_type, handler, execution_type)

    def unload_bundle(self):
        self.event_engine.stop()